﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiserBlogData.Entities;
using MiserBlogData.Entities.Type;
using NHibernate.Criterion;

namespace MiserBlogMVC.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index(List<MiserDB.DataHelp.QueryHelper.WhereKey> whereKey, List<SimpleExpression> whereItem,int? page)
        {
            MiserDB.DataHelp.QueryHelper data = new MiserDB.DataHelp.QueryHelper();
            UserVIP vip = data.GetData<UserVIP>(1);

            if (whereKey == null && whereItem == null)
            {
                vip.ArticleList = (from r in vip.ArticleList where r.State == (int)DataState.Release orderby r.Id descending select r).ToList();
            }
            else
            {
                vip.ArticleList = data.GetList<Article>(whereKey, whereItem, null).OrderByDescending(r => r.Id).ToList();
            }
            return View(vip);
        }

        public ActionResult Tag(int? id)
        {
            if (id.HasValue)
            {

                List<MiserDB.DataHelp.QueryHelper.WhereKey> whereKey = new List<MiserDB.DataHelp.QueryHelper.WhereKey>();
                whereKey.Add(new MiserDB.DataHelp.QueryHelper.WhereKey("Tags","tag"));

                

                List<SimpleExpression> whereItem = new List<SimpleExpression>();
                whereItem.Add(Restrictions.Eq("tag.Id", id.Value));
                whereItem.Add(Restrictions.Eq("State", (int)DataState.Release));


                return Index(whereKey,whereItem,1);
            }
            RedirectToAction("Index", new { controller = "", id = id.Value });
            return Index(null, null,1);
        }

        public ActionResult Categories(int? id)
        {
            if (id.HasValue)
            {

                List<MiserDB.DataHelp.QueryHelper.WhereKey> whereKey = new List<MiserDB.DataHelp.QueryHelper.WhereKey>();
                whereKey.Add(new MiserDB.DataHelp.QueryHelper.WhereKey("Categories", "categories"));

                List<SimpleExpression> whereItem = new List<SimpleExpression>();
                whereItem.Add(Restrictions.Eq("categories.Id", id.Value));
                whereItem.Add(Restrictions.Eq("State", (int)DataState.Release));


                return Index(whereKey, whereItem,1);
            }

            return Index(null, null,1);
        }
    }
}
